import GameData_blxf from "../GameData/GameData";
import { EnumManager_blxf } from "../Manager/EnumManager";
import PageManager_blxf from "../Manager/PageManager";
import { PlatformManager_blxf } from "../Manager/PlatformManager";
import AudioModule_blxf from "../Module/AudioModule";
import { ConfigModule_blxf } from "../Module/ConfigModule";
import { LoadModule_blxf } from "../Module/LoadModule";
import { NumModule_blxf } from "../Module/NumModule";
import { UtilsModule_blxf } from "../Module/UtilsModule";


const {ccclass, property} = cc._decorator;

/** 主玩法通用弹窗（空间不足、库存不足、老板别走） */

@ccclass
export default class CommonVendorPanel_blxf extends cc.Component {

    @property({ type: cc.Node, tooltip: '遮罩' })
    maskNode : cc.Node = null;

    @property({ type: cc.Node, tooltip: '关闭按钮' })
    closeBtn : cc.Node = null;

    @property({ type: cc.Node, tooltip: '标题盒子' })
    titleBox : cc.Node = null;

    @property({ type: cc.Sprite, tooltip: '贴图' })
    imgSpr : cc.Sprite = null;

    @property({ type: cc.Node, tooltip: '描述文字' })
    msgText : cc.Node = null;

    @property({ type: cc.Node, tooltip: '描述文字2' })
    msgText2 : cc.Node = null;

    @property({ type: cc.Node, tooltip: '描述文字3' })
    msgText3 : cc.Node = null;

    @property({ type: cc.Node, tooltip: '按钮1文字' })
    btnText_1 : cc.Node = null;

    @property({ type: cc.Node, tooltip: '按钮2文字' })
    btnText_2 : cc.Node = null;

    @property({ type: cc.Sprite, tooltip: '货币图标' })
    iconSpr : cc.Sprite = null;

    @property({ type: cc.Node, tooltip: '货币数量' })
    iconNumText : cc.Node = null;

    @property({ type: cc.Sprite, tooltip: '视频图标' })
    icon2Spr: cc.Sprite = null;

    @property({ type: cc.Node, tooltip: '视频图标' })
    videoNode : cc.Node = null;

    @property({ type: cc.Node, tooltip: '视频按钮' })
    videoBtn: cc.Node = null;

    @property({ type: cc.Node, tooltip: '消耗按钮' })
    consumeBtn: cc.Node = null;


    @property({ type: cc.Node, tooltip: '底部设置界面' })
    btmSetPanel: cc.Node = null;

    @property({ type: cc.Node, tooltip: '重置游戏按钮' })
    restartBtn: cc.Node = null;


    @property({ type: cc.Node, tooltip: '弱引导动画' })
    guiAnimation : cc.Node = null;
    @property({ type: cc.Node, tooltip: '手指' })
    handNode     : cc.Node = null;

    @property({ type: cc.Node, tooltip: '客人QQ' })
    qqTipsNode: cc.Node = null;


    @property({ tooltip: '传入参数' })
    _options : any = null;

    @property({ tooltip: '货币类型' })
    _iconType: number = -1;

    @property({ tooltip: '货币数量' })
    _iconNum: number = 0;

    @property({ tooltip: '选择点击按钮类型' })
    _touchBtnType: number = -1;

    @property({ tooltip: '是否被点击' })
    _isClick: boolean = false;

    @property({ tooltip: '手指动画' })
    _handTween: cc.Tween = null;

    @property({ tooltip: '是否分享' })
    _isShare: boolean = false;

    @property({ tooltip: '分享id' })
    _shareId: number = null;

    @property({ tooltip: '获取金币数' })
    _goldNum: number = 0;

    @property({ tooltip: '标题文字' })
    _titleText: string = null;

    // LIFE-CYCLE CALLBACKS:

    onLoad () {
        UtilsModule_blxf.instance.setNodeSize_blxf( this.maskNode );
    }

    start () {

    }

    // update (dt) {}

    initData_blxf ( options: any ) {
        this._options = options;

        this._goldNum = 0;

        this.updateUI();

        this.onReportEvent_blxf( -1 );
    }

    updateUI () {
        let { type, consumeType, consumeNum, _isGui1 } = this._options;

        /** 是否显示设置界面 */
        let _isShowSet: boolean = [ 3, 5 ].includes( type );
        /** 库存不足隐藏关闭按钮 */
        let _isHideCloseBtn: boolean = [ 2, 5 ].includes( type );
        /** 是否显示重置游戏 */
        let _isShowRestart: boolean = [ 3, 5 ].includes( type );

        let _title: string, _atlasName: string, _imgName: string, _imgUpdateY: number = 0;
        let _msg: string, _msg2: string, _msg3: string, _btnText1: string, _btnText2: string;
        let _iconType: number, _iconNum: number, _isVideo: boolean, _isConsumeBtn: boolean = true, _isVideoBtn: boolean = true;
        let _isShowQQ: boolean = false;

        // 1空间不足、2库存不足、3老板别走、4老板留步、5温馨提示、6老板慎重、7收益双倍、8加速售卖、9金币不足
        switch ( type ) {
            case 1 : 
                this._shareId = 1;
                this._isShare = !_isGui1 && !GameData_blxf.instance.onCheckShareId_blxf( this._shareId );
                _iconType = consumeType || 2, _iconNum = consumeNum, _isVideo = false && !_isGui1 && !this._isShare, _isConsumeBtn = false;
                _title = '空间不足', _atlasName = 'UITC', _imgName = 'CT_Elt_KongJianBuZu';
                _msg = '摊位已满，需要及时整理', _btnText1 = '卸货整理', _btnText2 = '卸货整理';
                break;

            case 2 : 
                _iconType = consumeType || 1, _iconNum = consumeNum, _isVideo = true, _imgUpdateY = 20;
                _title = '库存不足', _atlasName = 'UITC', _imgName = 'CT_Elt_KuCunBuZu';
                _msg = '仓库空空，请及时补货！', _btnText1 = '花钱补货', _btnText2 = '免费补货';
                break;

            case 3 : 
                this._shareId = 1;
                this._isShare = !GameData_blxf.instance.onCheckShareId_blxf( this._shareId );
                _iconType = -1, _iconNum = 0, _isVideo = !this._isShare, _isShowQQ = true;
                _title = '老板别走', _atlasName = 'UITC', _imgName = 'CT_Elt_LaoBanBeiZou';
                _msg = '你会成功的，千万别放弃！', _btnText1 = '返回首页', _btnText2 = '卸货整理';
                break;

            case 4 : 
                _iconType = 9, _iconNum = 0, _isVideo = false;
                _title = '老板留步', _atlasName = 'UITC', _imgName = 'CT_Elt_LaoBanBeiZou';
                _msg = '马上就要成功了，放弃可惜了噻！老板，试一哈整理嘛！', _btnText1 = '卸载整理', _btnText2 = '算了吧';
                break;
            
            case 5 :
                _iconType = -1, _iconNum = 0, _isVideo = true;
                _title = '温馨提示', _atlasName = '', _imgName = '';
                _msg = '摊主您有未完成的生意', _msg2 = '是否继续？', _btnText1 = '继续游戏', _btnText2 = '卸货整理';
                break;
            
            case 6 :
                _iconType = -1, _iconNum = 0, _isVideo = false;
                _title = '老板慎重', _atlasName = 'UITC', _imgName = 'CT_Elt_LaoBanBeiZou';
                _msg = '重置将清空当前关卡进度'/*, _msg2 = '需重头开始玩'*/, _btnText1 = '确定重置', _btnText2 = '不想重置';
                break;
            
            case 7 :
                _iconType = 9, _iconNum = 0, _isVideo = true, _isConsumeBtn = false;
                _title = '收益双倍', _atlasName = 'AutoUI', _imgName = 'Elt_shuangbei';
                _msg = '十分钟内，交易额双倍', _btnText2 = ' 接 受';
                break;

            case 8 :
                _iconType = 9, _iconNum = 0, _isVideo = true, _isConsumeBtn = false;
                _title = '加速售卖', _atlasName = 'AutoUI', _imgName = 'Elt_jiasutu';
                _msg = '售卖速度加快两倍, 持续10分钟', _btnText2 = ' 接 受';
                break;
            
            case 9 :
                let _goldVideoNum: number = GameData_blxf.instance.getGoldVideoNum_blxf();
                _iconType = -1, _iconNum = 0, _isVideo = true, _isVideoBtn = _goldVideoNum > 0;
                _title = '金币不足', _atlasName = 'UITC', _imgName = 'JBBZ';
                this._goldNum = UtilsModule_blxf.onVideoGoldNum_blxf();
                _msg = '哎呀，金库告急啦', _msg3 = _isVideoBtn ? `观看广告即可免费获得_${this._goldNum}_金币` : `快去给客人送货，赚取金币吧！__`, _btnText1 =  '送货获取', _btnText2 = '免费获取';
                break;
        }

        this._iconNum = _iconNum;
        this._iconType = _iconType;
        this._titleText = _title;
        
        this.videoNode.active = _isVideo || this._isShare;
        this.imgSpr.node.y += _imgUpdateY;

        this.closeBtn.active = !_isHideCloseBtn;

        this.onUpdateIcon_blxf();
        this.onUpdateTitle_blxf( _title );
        _imgName && this.onUpdateImg_blxf( _atlasName, _imgName );

        UtilsModule_blxf.instance.setText_blxf( this.msgText, _msg );
        UtilsModule_blxf.instance.setText_blxf( this.btnText_1, _btnText1 );
        UtilsModule_blxf.instance.setText_blxf( this.btnText_2, _btnText2 );

        // 红字部分
        if ( this.msgText2 ) {
            this.msgText2.active = !!_msg2;
            _msg2 && UtilsModule_blxf.instance.setText_blxf( this.msgText2, _msg2 );
        }

        // 金币不足部分
        if ( this.msgText3 && _msg3 ) {
            let _textList: string[] = _msg3.split('_');
            this.msgText3.active = !!_msg3;
            this.msgText3.children.forEach( (textItem, textIndex) =>{
                let _text: string = _textList[ textIndex ];
                UtilsModule_blxf.instance.setText_blxf( textItem, _text );
                textItem.active = !!_text;
            })
        }

        if ( this.btmSetPanel ) {
            this.btmSetPanel.active = _isShowSet;
        }

        if ( this.restartBtn ) {
            this.restartBtn.active = _isShowRestart;
            this.restartBtn.y -= ConfigModule_blxf.instance.checkH / 3;
        }

        if ( this.guiAnimation ) {
            this.guiAnimation.active = _isGui1;

            _isGui1 && this.onHandAnimation_blxf();
        }

        if ( this.consumeBtn ) {
            this.consumeBtn.active = _isConsumeBtn;
        }
        if ( this.videoBtn ) this.videoBtn.active = _isVideoBtn;

        if ( this.qqTipsNode ) {
            this.qqTipsNode.active = _isShowQQ;
            this.qqTipsNode.y -= ConfigModule_blxf.instance.checkH / 3;
        }
    }

    onUpdateTitle_blxf ( _title: string ) {
        let _titleArr: string[] = _title.split('');
        this.titleBox.children.forEach( (titleItem, index) =>{
            let _titleText: string = _titleArr[ index ];
            UtilsModule_blxf.instance.setText_blxf( titleItem, _titleText );
        })
    }

    onUpdateImg_blxf ( path: string, imgName: string ) {
        let _options: object = { path, imgName, imgSpr: this.imgSpr };
        LoadModule_blxf.instance.LoadImg_blxf( _options );

        let { type } = this._options;
        this.imgSpr.node.scale = type == 9 ? 0.5 : 1;
    }

    onUpdateIcon_blxf () {
        UtilsModule_blxf.instance.loadIconImg_blxf( this._iconType, this.iconSpr );
        UtilsModule_blxf.instance.setText_blxf( this.iconNumText, `x${this._iconNum}` );

        let _isShow: boolean = this._iconType != -1;
        let _isVideo: boolean = this._iconType == 9;
        this.iconSpr.node.active = _isShow;
        this.iconNumText.active = _isShow && !_isVideo;

        if ( this._isShare ) {
            let { NUM_SHARE } = EnumManager_blxf.gameNumType;
            UtilsModule_blxf.instance.loadIconImg_blxf( NUM_SHARE, this.icon2Spr );
        }
    }

    onTouchBtn1 () {
        let { type } = this._options;

        let _isNum: boolean = type == 3 || this._iconNum == 0;
        _isNum = _isNum || NumModule_blxf.instance.onCheckNum_blxf( this._iconType, this._iconNum, true );
        if ( !_isNum ) return;

        let _isVideo = this._iconType == 9;
        if ( !_isVideo ) return this.onTouchOk_blxf( 1 );

        // TODO 观看视频成功回调
        PlatformManager_blxf.instance.onPlayVideo_blxf(()=>{
            this.onTouchOk_blxf( 1 );

            // 完成激励视频埋点
            this.onCompleteVideoReportEvent_blxf();
        })
    }

    onTouchBtn2 () {
        let { type, _isGui1 } = this._options;
        let _isVideo: boolean = [ 1, 2, 3, 5, 7, 8, 9 ].includes(type);
        let _isFree = _isGui1;
        _isFree = true;

        // 分享
        if ( this._isShare && !_isFree ) {
            // 调用分享
            UtilsModule_blxf.onShareMessage( this._shareId );
            return cc.game.on(`onShareOK${this._shareId}_blxf`, this.onShareOk_blxf, this);
        }

        if ( !_isVideo ) return this.onTouchOk_blxf( 2 );

        // TODO 观看视频成功回调
        PlatformManager_blxf.instance.onPlayVideo_blxf(()=>{
            this.onTouchOk_blxf( 2 );

            !_isFree && this._shareId && GameData_blxf.instance.setVideoIdList_blxf( this._shareId );

            // 完成激励视频埋点
            !_isFree && this.onCompleteVideoReportEvent_blxf();
        }, _isFree)
    }

    /** 分享成功 */
    onShareOk_blxf () {
        this.onTouchOk_blxf( 2 );

        GameData_blxf.instance.setShareIdList_blxf( this._shareId );
    }

    /** 点击成功 */
    onTouchOk_blxf ( btnType: number ) {
        this._touchBtnType = btnType;

        btnType == 1 && this._iconType != -1 && NumModule_blxf.instance.updateNumByType( this._iconType, -this._iconNum );
        btnType == 1 && cc.game.emit('onUpdateTopNum_blxf');

        let { type, _isGui1 } = this._options;
        let { autoAddSpeedTime } = ConfigModule_blxf;
        switch ( type ) {
            case 1 :
                // cc.game.emit('onTouchRemoveShop_blxf');
                cc.game.emit('onTouchRestartShop_blxf');
                btnType == 2 && _isGui1 && GameData_blxf.instance.setIsGuiRemoveStart_blxf();
                break;

            case 2 :
                GameData_blxf.instance.addShopNum_blxf();
                cc.game.emit('onRestartBtnShopNum');
                break;

            case 3 :
            case 4 :
                // btnType == 1 && cc.game.emit('onTouchRestartShop_blxf');
                btnType == 1 && AudioModule_blxf.instance.playMainBgm_blxf();
                if ( btnType == 2 ) {
                    // cc.game.emit('onTouchRemoveShop_blxf');
                    cc.game.emit('onTouchRestartShop_blxf'); 
                }
                break;
            case 5 :
                // btnType == 2 && cc.game.emit('onEntryGameClear_blxf');
                if ( btnType == 2 ) {
                    // cc.game.emit('onTouchRemoveShop_blxf');
                    cc.game.emit('onTouchRestartShop_blxf'); 
                }
                cc.game.emit('onPlayGuestAudio_blxf');
                break;
            
            case 6 :
                btnType == 1 && cc.game.emit('onMainRestartGame_blxf');
                break;
            
            case 7 :
                this.loadAutoDoubleSpine_blxf();
                GameData_blxf.instance.updateAutoDoubleRewardTime_blxf( autoAddSpeedTime );
                break;

            case 8 :
                this.loadAutoDoubleSpine_blxf();
                GameData_blxf.instance.updateAutoDoubleSpeedTime_blxf( autoAddSpeedTime );
                break;
            case 9 :
                btnType == 1 && cc.game.emit('onUpdatePage_blxf');
                btnType == 2 && this.onReceiveGold_blxf();
                break;
        }

        // 事件埋点
        this.onReportEvent_blxf( btnType );

        this.onClose();
    }

    /** 免费领取金币 */
    onReceiveGold_blxf () {
        NumModule_blxf.instance.updateNumByType( 1, this._goldNum );

        GameData_blxf.instance.updateGoldVideoNum_blxf();

        cc.game.emit('onBuildLvUpRestartUI_blxf');
    }

    /** 加载加速动画 */
    loadAutoDoubleSpine_blxf () {
        let { type } = this._options;
        let { SPINE_AUTO_DOUBLE_REWARD, SPINE_AUTO_DOUBLE_SPEED, SPINE_AUTO_HORN } = EnumManager_blxf.gameSpineType;

        // 横幅
        let _spineId: string = type == 7 ? SPINE_AUTO_DOUBLE_REWARD : SPINE_AUTO_DOUBLE_SPEED;
        UtilsModule_blxf.instance.createSpine_blxf( _spineId, cc.find('Canvas/game_panel'), ( skeleton: sp.Skeleton )=>{
            AudioModule_blxf.instance.playAudio_blxf( EnumManager_blxf.gameAudioType.AUDIO_DRAGON_EXP );

            // 喇叭
            UtilsModule_blxf.instance.createSpine_blxf( SPINE_AUTO_HORN, cc.find('Canvas/game_panel'), ( skeleton: sp.Skeleton )=>{
                
            });
        });
    }

    /** 重置游戏 */
    onTouchRestartGame () {
        if ( this._isClick ) return;
        UtilsModule_blxf.instance.setClick_blxf( this );

        PageManager_blxf.instance.loadCommonVendorPanel_blxf({
            type    : 6,
            touchCB : ( options: any ) =>{
                let { isRestart } = options;
                 isRestart && this.onClose();
                !isRestart && this.updateNode_blxf( true );
            }
        })

        this.updateNode_blxf( false );

        // 事件埋点
        this.onReportEvent_blxf( 3 );
    }

    /** 离开 */
    onClose ( e: cc.Event.EventTouch = null ) {
        let { type, touchCB } = this._options;

        e && this.onReportEvent_blxf( 0 );

        let _isCloseVendor: boolean = !e && this._touchBtnType == 1 &&  [ 3, 4 ].includes( type );
        _isCloseVendor && cc.game.emit('onCloseVendorPanel');

        touchCB && touchCB({
            isRestart: type == 6 && this._touchBtnType == 1
        })

        this.node.destroy();
    }

    /** 更新页面显隐 */
    updateNode_blxf ( isShow: boolean ) {
        this.node.opacity = isShow ? 255 : 0;
    }


    /** 上报自定义事件 */
    onReportEvent_blxf ( btnType: number ) {
        let { type } = this._options;

        // type     : 1空间不足、2库存不足、3老板别走、4老板留步、5温馨提示、6老板慎重
        // btnType  : 0关闭按钮、1按钮上、2按钮下、3重置游戏

        let _eventId: number;
        switch ( type ) {
            // case 1 :
            //     if ( btnType == 0 ) _eventId = 3113;
            //     if ( btnType == 1 ) _eventId = 3114;
            //     if ( btnType == 2 ) _eventId = this._isShare ? 3115 : 3112;
            //     if ( btnType == -1 )_eventId = 3110;
            //     break;
            
            // case 2 :
            //     let _addShopNum: number = GameData_blxf.instance.getAddShopNum_blxf();
            //     let _isFrist: boolean = _addShopNum == 1;
            //     if ( btnType == 1 ) _eventId = _isFrist ? 1101 : 4116;
            //     if ( btnType == 2 ) _eventId = _isFrist ? 1102 : 4115;
            //     if ( btnType == -1 )_eventId = _isFrist ? 1100 : 4110;
            //     break;
            
            // case 3 :
            //     if ( btnType == 0 ) _eventId = 5119;
            //     if ( btnType == 1 ) _eventId = 5117;
            //     if ( btnType == 2 ) _eventId = this._isShare ? 3115 : 5118;
            //     if ( btnType == 3 ) _eventId = 5120;
            //     if ( btnType == -1 )_eventId = 5110;
            //     break;
            
            // case 5 :
            //     if ( btnType == 1 ) _eventId = 2103;
            //     if ( btnType == 2 ) _eventId = 2104;
            //     if ( btnType == 3 ) _eventId = 2105;
            //     if ( btnType == -1 )_eventId = 2100;
            //     break;
            
            case 6 :
                // if ( btnType == 0 ) _eventId = 6123;
                if ( btnType == 1 ) _eventId = 341;
                // if ( btnType == 2 ) _eventId = 6122;
                break;
            
            // case 7 :
            //     if ( btnType == -1 ) _eventId = 7001;
            //     if ( btnType ==  2 ) _eventId = 7002;
            //     break;
            
            // case 8 :
            //     if ( btnType == -1 ) _eventId = 7011;
            //     if ( btnType ==  2 ) _eventId = 7012;
            //     break;

            // case 9 :
            //     if ( btnType == -1 && this.videoBtn.active ) _eventId = 6201;
            //     if ( btnType == 2 ) _eventId = 6202;
            //     break;

        }

        _eventId && UtilsModule_blxf.onReportEvent_blxf( 2, _eventId, true );
    }

    /** 完成激励视频埋点 */
    onCompleteVideoReportEvent_blxf () {
        UtilsModule_blxf.onReportEvent_blxf( 2, 311, false, { "adv_game1": this._titleText } );
    }

    /** 手指动画 */
    onHandAnimation_blxf () {
        let _t1 = 0.4, _t2 = 0.4;
        let _scale1 = 0.85, _scale2 = 1;
        let _tween = cc.tween( this.handNode )
        .to(_t1, { scale: _scale1 })
        .to(_t2, { scale: _scale2 })

        this.handNode.scale = 1;
        this._handTween = cc.tween( this.handNode ).repeatForever( _tween ).start();
    }
    
    onDestroy () {
        cc.game.removeAll( this );
    }
}
